// pages/collect/collect.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data:[
      {
        id:'1',
        name:'某某某',
        work:'ui设计师',
        company:'河北森竺软件科技有限公司',
        pic:'/img/touxiang.png',
      },
    ],
    arrData:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.jiazai();
  },
  // onShow(options){
  //   this.jiazai();
  // },
  jiazai:function(){
    let that= this;
    var user_id = wx.getStorageSync('user_id');
    var arr=[];
    let arrData = that.data.arrData;
    wx.request({
      url: `http://127.0.0.1:2020/api/get_collected?post_col_id=${user_id}`,
      method:'GET',
      header:{'content-type':'application/JSON;charset=UTF-8'},
        success:function(res){
        console.log(res.data)
        for(var i=0;i<res.data.length;i++){
          arr[i]=res.data[i].get_col_id;
        }
        console.log(arr)
        that.setData({
          data:arr,
        })
        for(var i = 0;i<that.data.data.length;i++){
          wx.request({
            url: `http://127.0.0.1:2020/api/login?id=${that.data.data[i]}`,
            method:'GET',
            header:{'content-type':'application/JSON;charset=UTF-8'},
            success:function(res){
              that.data.arrData.push(res.data);
              that.setData({
                [arrData[i]]:that.data.arrData,
              })
              that.setData({
                data:that.data.arrData
              })  
            }
          })
        }   
        }
    })
  },
  
  card_show:function(e){
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: `/pages/card_show/card_show?id=${id}`,
    })
  },

  btn:function(e){
    var that= this;
    var del_id = e.target.id;
    var user_id = wx.getStorageSync('user_id')
    wx.showModal({
      title:'温馨提示',
      content:'是否取消收藏该用户',
      confirmText:'确认',
      cancelText:'取消',
      success:(res)=>{
        wx.request({
          url: `http://127.0.0.1:2020/api/del_collected?post_col_id=${user_id}&get_col_id=${del_id}`,
          method:'GET',
          header:{'content-type':'application/JSON;charset=UTF-8'},
          success:function(res){
            console.log(res.data);
            if(res.data=='取消收藏成功'){
              var flag=[];
              for(var i=0;i<that.data.data.length;i++){
                if(that.data.data[i].id!=del_id){
                  flag.push(that.data.data[i])
                }
              }
              that.setData({
                data:flag,
              })
            }
          },
        })
      }
      })
    
  },
  
})